﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
/*
 * 
 *   基本信息的处理
 * 
 */
namespace TRPOP.CMS.Template.GenerateCS
{
    /// <summary>
    /// 将cms标签替换为csharp语言
    /// </summary>
    partial class ConvertTags
    {
        /// <summary>
        /// 解析指定的字符串
        /// </summary>
        /// <param name="strTemp"></param>
        /// <param name="strSpaces"></param>
        /// <param name="intIndexRow"></param>
        /// <returns></returns>
        public static string ConvertTagsAccess(string strTemp, ref string strSpaces, int intIndexRow)
        {
            //去掉换行 回车 符合
            strTemp = strTemp.Replace("\n", string.Empty).Replace("\r", string.Empty);

            //不处理 include 标签,已经在前面的代码处理过了
            string strReturn = string.Empty;

            #region 处理模板数据

            //循环将每个标签转换为cSharp代码
            int index = -1;
            Match oMatch = GetTopOneAnalysisTag(strTemp, ref index);

            if (!oMatch.Success)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.AppendLine(\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strTemp), Environment.NewLine);
            }
            else
            {
                while (oMatch.Success && index != -1)
                {
                    switch (index)
                    {
                        case 1:  // [$var] [$var.var] [$var.0.var]  之类的标签
                            strReturn += ConvertTag_Var(ref strTemp, oMatch, strSpaces, true);
                            break;
                        case 0: //  <(?<TagName>(?:include|cms|op|var|loop|\/loop|if|elseif|else|\/if>))[\s>]
                            /*  <include  之前已经解析过了
                             *  <cms 或<cms> 
                             *  <op 或<op>
                             *  <var 或<var>
                             *  <loop 或</loop>
                             *  <if 或<elseif 或<else>或</if>
                             */
                            switch (oMatch.Groups["TagName"].Value.Trim().ToLower())
                            {
                                case "var": // <var
                                    strReturn += ConvertTag_DefineVar(ref strTemp, oMatch, strSpaces, true);
                                    break;
                                case "op":  // <op 
                                    strReturn += ConvertTag_OP(ref strTemp, oMatch, strSpaces, true);
                                    break;
                                case "cms": // <cms 
                                    strReturn += ConvertTag_CMS(ref strTemp, oMatch, strSpaces, true);
                                    break;
                                case @"/loop":
                                case "loop":
                                    strReturn += ConvertTags_Loop(ref  strTemp, oMatch, ref strSpaces);
                                    break;
                                case "if"://<if
                                case "elseif"://<elseif
                                case "else"://<else>
                                case "/if"://</if>
                                    strReturn += ConvertTags_If(ref  strTemp, oMatch, ref strSpaces);
                                    break;
                                default:
                                    strTemp = strTemp.Replace(oMatch.Value, "");
                                    break;
                            }
                            break;
                        case 2: // 解析函数  [@date('yyyy-MM-dd',$PublishDate)]
                            strReturn += ConvertTag_Func(ref strTemp, oMatch, strSpaces, true);
                            break;
                    }
                    oMatch = GetTopOneAnalysisTag(strTemp, ref index);
                }
                if (!strTemp.IsEmpty())
                {
                    strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strTemp), Environment.NewLine);
                }
                if (strReturn.Contains("oTemplatePageInfo.strBuildHtml.Append"))
                {
                    //html 进入新的一行
                    strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.AppendLine();{0}", Environment.NewLine);
                }
            }

            #endregion

            //加上代码注释
            string strPreStr = strSpaces + Environment.NewLine + strSpaces + string.Format("/*   解析模板页的第{0}行数据   */{1}", intIndexRow.ToString("000"), Environment.NewLine);
            strReturn = strPreStr + strReturn;

            return strReturn;
        }

    }
}